package ch1.refactors;

import java.util.Enumeration;
import java.util.Vector;

public class Customer {

    private String _name;

    private Vector _rentals = new Vector();

    public Customer(String name){
        this._name = name;
    }

    public String getName(){
        return this._name;
    }

    public String statement(){
        Enumeration<Rental> rentals = _rentals.elements();
        String result = "Rental Record for" + getName() + "\n";

        while (rentals.hasMoreElements()){
            Rental each = rentals.nextElement();
            result += "\t" + each.getMovie().getTitle() + "\t" +
                    String.valueOf(each.getCharge()) + "\n";
        }
        result += "Amount owed is " + String.valueOf(getTotalCharge()) + "\n";
        result += "Yout earned " + String.valueOf(getTotalFrequentRenterPoints())+
                " frequent renter points";
        return result;
    }

    /**
     * 处理掉临时变量
     * @return
     */
    private double getTotalCharge(){
        double result = 0;
        Enumeration<Rental> rentals = _rentals.elements();
        while (rentals.hasMoreElements()) {
            Rental each = rentals.nextElement();
            result += each.getCharge();
        }
        return result;
    }

    private int getTotalFrequentRenterPoints(){
        int result = 0;
        Enumeration<Rental> rentals = _rentals.elements();

        while (rentals.hasMoreElements()){
            Rental each = rentals.nextElement();
            result += each.getFrequentRenterPoints();
      }
        return result;
    }

}
